import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gameplay_flutter/lib_runtime/logger.dart';
import 'package:provider/provider.dart';
import '../lib_baseview/lib_baseview.dart';
import '../lib_network/http_api.dart';
import '../lib_network/http_helper.dart';
import '../lib_runtime/global_config.dart';
import 'platforms/third_login_platform_interface.dart';

class OSLoginController extends GetxController {
  bool protocolSelect = false;
  // late String areaCode;
  // late TextEditingController textController;
  // late LoginPageModel _loginModel;

  // late List<IThirdLoginPlatform> mainLoginPlatforms;
  // late List<IThirdLoginPlatform> subLoginPlatforms;

  bool loadingPlatforms = true;

  @override
  void onInit() {
    super.onInit();
    // ThirdLoginPlatformFactory.ins.addLoginPlatform(PhoneLoginPlatform());
    // ThirdLoginPlatformFactory.ins.listenLoginEvent.listen((event) {
    //   if (event.suc) {
    //     authSuccessWithThirdPlatform(event.authRsp?.authed,
    //         loginPlatform: event.platform);
    //   } else {
    //     showToast(event.errorMsg ?? T.current!.server_exception);
    //   }
    // });
  }

  @override
  void onReady() {
    super.onReady();
    _loadLoginPlatforms();
  }

  @override
  void onClose() {
    super.onClose();
  }

  changeProtocolSelect() {
    protocolSelect = !protocolSelect;
    update(['protocolSelect']);
  }

  changeAreaCode(String code) {
    // areaCode = code;
    // CacheUtil.set(CacheKeys.lastLoginAreaCode, areaCode);
    // update();
  }

  // bool get validMobile => StringUtil.isMobile(textController.text, areaCode: areaCode);

  loginAction() async {
    // if (!validMobile) return;
    // if (!protocolSelect) {
    //   AlertShow.showPrivacyAgree(rootNavigatorState.currentContext!, T.current!.agree_get_verifition_code, () {
    //     changeProtocolSelect();
    //     _pushVerifySmsPage();
    //   });
    //   return;
    // }
    // _pushVerifySmsPage();

    // if (!userAgreementChecked.value) {
    //   Get.dialog(AutoAgreeWidget(
    //     onCallBack: () {
    //       userAgreementChecked.value = true;
    //       visitorLogin();
    //     },
    //   ));
    //   return;
    // }
    // var response = await HttpHelper.instance().post(
    //   HttpApi.getcountry,
    //   body: {"loginType": "device"},
    //   showLoading: true,
    //   showToast: true,
    // );
    // if (response.isSuccess) {
    //   print("");

    //   // UserModelEntity userEntity = UserModelEntity.fromJson(response.result);
    //   // UserCenter.update(userEntity);

    //   // if (userEntity.gender != null) {
    //   //   Get.offAllNamed(FHRouteName.tab);
    //   // } else {
    //   //   Get.offAllNamed(FHRouteName.guide_sex);
    //   // }

    //   // FHAnalyticsReporter.track(FHTrackEvent.log_withvisitor_success, null);
    // }
  }

  // thirdLoginAction(IThirdLoginPlatform platform) async {
  //   if (!protocolSelect) {
  //     dynamic re = await AlertShow.showPrivacyAgree(
  //         rootNavigatorState.currentContext!, T.current!.agree_login);
  //     if (re == true) {
  //       protocolSelect = true;
  //       update(['protocolSelect']);
  //       platform.login(rootNavigatorState.currentContext!);
  //     }
  //   } else {
  //     platform.login(rootNavigatorState.currentContext!);
  //   }
  // }

  // authSuccessWithThirdPlatform(AuthRsp? resultData,
  //     {LoginPlatform? loginPlatform}) async {
  //   try {
  //     showLoadingDialog();
  //     var result = await LoginManager.instance.loginSuccessUpdateInfo(
  //         LoginType.autoLogin, resultData?.token ?? '',
  //         loginPlatform: loginPlatform);
  //     if (result) {
  //       LoginManager.instance.loginSuccessJump();
  //     } else {
  //       showToast(T.current!.server_exception);
  //     }
  //   } catch (e) {
  //     showToast(e.toString());
  //   } finally {
  //     dismissLoadingDialog();
  //   }
  // }

  void _loadLoginPlatforms() async {
    // var rsp = await LoginApi.instance.getLoginPlatforms();
    // mainLoginPlatforms = [];
    // subLoginPlatforms = [];
    // if (rsp.isSuccess && rsp.data != null) {
    //   for (String platform in rsp.data!.mainPlatforms) {
    //     IThirdLoginPlatform? thirdLoginPlatform =
    //         ThirdLoginPlatformFactory.ins.getPlatform(platform);
    //     if (thirdLoginPlatform != null) {
    //       mainLoginPlatforms.add(thirdLoginPlatform);
    //     }
    //   }
    //   for (String platform in rsp.data!.subPlatforms) {
    //     IThirdLoginPlatform? thirdLoginPlatform =
    //         ThirdLoginPlatformFactory.ins.getPlatform(platform);
    //     if (thirdLoginPlatform != null) {
    //       subLoginPlatforms.add(thirdLoginPlatform);
    //     }
    //   }
    // }
    // if (mainLoginPlatforms.isEmpty) {
    //   _setupDef();
    // }

    loadingPlatforms = false;
    update();
  }

  void _setupDef() {
    // for (LoginPlatform platform in _defaultMainPlatforms) {
    //   IThirdLoginPlatform? thirdLoginPlatform =
    //       ThirdLoginPlatformFactory.ins.getPlatform(platform.name);
    //   if (thirdLoginPlatform != null) {
    //     mainLoginPlatforms.add(thirdLoginPlatform);
    //   }
    // }
    // for (LoginPlatform platform in _defaultSubPlatforms) {
    //   IThirdLoginPlatform? thirdLoginPlatform =
    //       ThirdLoginPlatformFactory.ins.getPlatform(platform.name);
    //   if (thirdLoginPlatform != null) {
    //     subLoginPlatforms.add(thirdLoginPlatform);
    //   }
    // }
  }
}

var _defaultMainPlatforms = [
  if (Platform.isIOS) LoginPlatform.apple,
  LoginPlatform.google,
];

var _defaultSubPlatforms = [
  if (GlobalConfig.isDebug) LoginPlatform.phone,
];
